package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.modular.common.service.SysAuthorityService;
import cn.e4j.modular.crm.entity.Gwai760;
import cn.e4j.modular.crm.service.impl.Gwai760ServiceImpl;
import cn.e4j.modular.crm.service.impl.Gwai780ServiceImpl;
import cn.e4j.modular.crm.service.impl.Gwai790ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 季返报表
 * Created by miaoyl on 2019-05-07
 */
@Controller
@RequestMapping("/crm/gwai790")
public class Gwai790Controller extends BaseController {

    @Autowired
    private Gwai790ServiceImpl gwai790Service;

    /**
     * 跳转季返报表管理页面
     */
    @RequiresPermissions("crm:gwai790:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwai790.html";
    }

    /**
     * 查询季返报表信息列表
     */
    @RequestMapping("/getList")
    @RequiresPermissions("crm:gwai790:init")
    @ResponseBody
    public ResponseData getList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = new ArrayList<>();
        if(inDto.getString("COMPANY") == null || inDto.getString("COMPANY") == ""){
            return new PageReData(page, result);
        }
        result = gwai790Service.getGwddlist(page, inDto);
        if(result.size() > 0){
            String DOCDATE_START = inDto.getString("DOCDATE_START").replaceAll("-","");
            String DOCDATE_END = inDto.getString("DOCDATE_END").replaceAll("-","");
            inDto.put("DOCDATE_START",DOCDATE_START);
            inDto.put("DOCDATE_END",DOCDATE_END);
            for (int i = 0;i < result.size(); i++){
                Dto gwddDto = result.get(i);
                inDto.put("GWDD012",gwddDto.getString("GWDD012"));//客户编号
                Dto gwalDto = this.gwai790Service.getGwal(inDto);
                if(gwalDto != null){
                    result.get(i).put("GWAL013_NAME",gwalDto.getString("GWADL004"));//省份(联系人所属地区)
                }
                BigDecimal T1 = new BigDecimal(0);
                BigDecimal T2 = new BigDecimal(0);
                BigDecimal T4 = new BigDecimal(0);
                BigDecimal T5 = new BigDecimal(0);
                Dto maDto = this.gwai790Service.getMA041(inDto);
                if(maDto != null){
                    result.get(i).put("MA041",maDto.getString("MA041"));//结算方式
                }
                if("1".equals(gwddDto.getString("GWDE011")) || "3".equals(gwddDto.getString("GWDE011"))){//返利编号净发货额
                    Dto tgiDto = this.gwai790Service.getSumTgi(inDto);
                    result.get(i).put("FHJE",tgiDto.getBigDecimal("TG_SUM"));//发货金额
                    result.get(i).put("THJE",tgiDto.getBigDecimal("TI_SUM"));//退货金额
                    T1 = tgiDto.getBigDecimal("TG_SUM").subtract(tgiDto.getBigDecimal("TI_SUM"));
                }else {
                    result.get(i).put("FHJE",0);
                    result.get(i).put("THJE",0);
                }
                Dto tkDto = this.gwai790Service.getSumTk(inDto);
                result.get(i).put("HKJE",tkDto.getString("TK_SUM"));//回款金额
                Dto YQHK = this.gwai790Service.getYQHK(inDto);
                result.get(i).put("YQHK",YQHK.getString("YQHK"));//逾期回款
                T5 = YQHK.getBigDecimal("YQHK");
                if("1".equals(gwddDto.getString("GWDE012")) || "3".equals(gwddDto.getString("GWDE012"))){//返利编号无返利发货额
                    Dto WFLFH = this.gwai790Service.getWFLFH(inDto);
                    result.get(i).put("WFLFH",WFLFH.getString("WFLFH"));//无返利发货
                    T2 = WFLFH.getBigDecimal("WFLFH");
                }else {
                    result.get(i).put("WFLFH",0);
                }
                if("1".equals(gwddDto.getString("GWDE014")) || "3".equals(gwddDto.getString("GWDE014"))){//返利编号铺货调整货额
                    Dto ph = this.gwai790Service.getPH(inDto);
                    result.get(i).put("PHFH",ph.getString("PHFH"));//铺货发货
                    result.get(i).put("PHHK",ph.getString("PHHK"));//铺货回款
                    result.get(i).put("PHWFL",ph.getString("PHWFL"));//铺货无返利
                    T4 = ph.getBigDecimal("PHFH").subtract(ph.getBigDecimal("PHHK"))
                            .subtract(ph.getBigDecimal("PHWFL"));
                }else {
                    result.get(i).put("PHFH",0);
                    result.get(i).put("PHHK",0);
                    result.get(i).put("PHWFL",0);
                }
                BigDecimal YJFHJE = T1.subtract(T2).subtract(T4).subtract(T5);
                result.get(i).put("YJFHJE",YJFHJE);
                BigDecimal ZCFLJE = YJFHJE.multiply(gwddDto.getBigDecimal("GWDD019")).divide(new BigDecimal(100));
                result.get(i).put("ZCFLJE",ZCFLJE);
            }
        }
        return new PageReData(page, result);
    }

    /**
     * 获取返利类型
     */
    @RequestMapping("/getGwdeData")
    @ResponseBody
    public ResponseData getGwdeData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = gwai790Service.getGwdelist(inDto);
        return new SuccessResponseData(result);
    }
}
